package com.ego.product.controller;

import java.util.Arrays;
import java.util.List;

import com.ego.common.vo.PageVo;
import com.ego.product.po.ProductAttrValue;
import com.ego.product.service.ProductAttrValueService;
import com.ego.common.vo.product.AttrRespVo;
import com.ego.product.vo.AttrSearchVo;
import com.ego.common.vo.product.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.product.service.AttrService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @author KamTang
 * @program: ego
 * @description 商品属性控制器
 * @date 2022-11-13 12:56:14
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    @GetMapping("/base/spu/{spuId}")
    public Result baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValue> entities = productAttrValueService.get(spuId);
        return Result.ok().put("data", entities);
    }

    /**
     * 搜索商品属性
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody AttrSearchVo search) {
        PageVo<AttrRespVo> page = attrService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/get/{attrId}")
    public Result get(@PathVariable("attrId") Long attrId) {
        AttrRespVo attr = attrService.get(attrId);
        return Result.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody AttrVo attr) {
        attrService.saveCascade(attr);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@Valid @RequestBody AttrVo attrVo) {
        attrService.update(attrVo);
        return Result.ok();
    }

    /**
     * 修改具体SPU信息
     */
    @PostMapping("/update/{spuId}")
    public Result update(@Valid @RequestBody List<ProductAttrValue> productAttrValues,
                         @PathVariable("spuId") Long spuId) {
        productAttrValueService.update(spuId, productAttrValues);
        return Result.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return Result.ok();
    }

}
